// pages/commoditydetails/commoditydetails.js
var utils = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            id: options.id,
            number: wx.getStorageSync("number")

        })
        that.categorylist(options.id);
        that.commentslist(options.id);
    },
    //商品详情
    categorylist: function(id) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/detail", {
            id: id
        }, function(res) {
            utils.hideLoading();
            that.setData({
                dataDetail: res.datas
            })
            WxParse.wxParse('article', 'html', res.datas.content, that, 5);
        })
    },
    //商品评价
    commentslist: function(id) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/comments_list", {
            be_id: id
        }, function(res) {
            utils.hideLoading();
            var commentsPost = []
            for (var i = 0; i <2; i++) {
                commentsPost.push(res.datas.list[i])
            }
            that.setData({
                comments:commentsPost,
                commentsAll:res.datas.list
            })
        })
    },
    //添加购物车
    addCarTap: function (e) {
        var that = this;
        var shuzi = wx.getStorageSync("number") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("number", number);
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false,   //购物车做删除时候用
            number: 1 //加减数量
        }
        utils.uniqueSearchRecord(dataInfo);
    },
    //浏览图片
    imgTap:function(e){
        var that=this;
        var index = e.currentTarget.dataset.index;
        wx.previewImage({
            current: e.currentTarget.dataset.http, // 当前显示图片的http链接
            urls: that.data.comments[index].image // 需要预览的图片http链接列表
        })
    },
    //跳转商品评价更多页面
    onEvaluateTap: function(e) {
        wx.navigateTo({
            url: '../commoditydetails/moreevaluatelist/moreevaluatelist?id='+this.data.id,
        })
    },
    //立即购买
    buyNowTap: function(e) {
        wx.navigateTo({
            url: '../confirmorder/confirmorder',
        })
    }
})